﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Gem_Tools;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_Controller.Gem_Tools;
using GE_Management_Tool;

namespace GE_Management_Tool.Category
{
    public partial class frmA002_Client_Edit_Form : frmBase, ICategory<CClient>
    {
        CClient m_objData;
        Boolean m_isNew = false;

        public frmA002_Client_Edit_Form()
        {
            InitializeComponent();
            CCommon_Function.Format_All(this);
        }

        public CClient Show_To_Create()
        {
            this.Text = "Create New Client Info";
            m_isNew = true;
            this.ShowDialog();

            return m_objData;
        }

        public CClient Show_To_Edit(CClient pClient)
        {
            m_isNew = false;
            m_objData = pClient;
            Assign_Obj_To_Control(pClient);
            this.Text = "Editing " + pClient.Client_Name + " Info";
            this.ShowDialog();

            return m_objData;
        }

        public void Assign_Control_To_Obj()
        {
            m_objData.Country = cbbCountry.Text.Trim();
            m_objData.Client_Name = txtClientName.Text.Trim();
            m_objData.Contacts_Info = txtContactsInfo.Text.Trim();
            m_objData.Note = txtNote.Text.Trim();
        }

        public void Assign_Obj_To_Control(CClient pobjData)
        {
            cbbCountry.Text = pobjData.Country.Trim();
            txtClientName.Text = pobjData.Client_Name.Trim();
            txtContactsInfo.Text = pobjData.Contacts_Info.Trim();
            txtNote.Text = pobjData.Note.Trim();
        }

        public string Check_Before_Save()
        {
            StringBuilder v_Sb = new StringBuilder();
            if (cbbCountry.Text == "")
            {
                v_Sb.AppendLine("- missing country info.");
            }
            if (txtClientName.Text == "")
            {
                v_Sb.AppendLine("- missing client name info.");
            }
            if (txtContactsInfo.Text == "")
            {
                v_Sb.AppendLine("- missing contacts info.");
            }

            return v_Sb.ToString();
        }

        /// <summary>
        /// do save data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                string error = "";
                error = Check_Before_Save();
                if (error != "")
                {
                    throw new Exception(error);
                }

                if (m_isNew == true)
                {
                    m_objData = new CClient();
                    Assign_Control_To_Obj();
                    CClient_Controller v_objCtrlClient = new CClient_Controller();
                    m_objData.Auto_ID = v_objCtrlClient.Insert_Client(m_objData);
                }
                else
                {
                    Assign_Control_To_Obj();
                    CClient_Controller v_objCtrlClient = new CClient_Controller();
                    v_objCtrlClient.Update_Client(m_objData);
                }

                XtraMessageBox.Show("Success !");
                this.Close();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK);
            }
        }

        public CClient Show_To_Edit(CClient pobjData, ref bool pisChange)
        {
            throw new NotImplementedException();
        }
    }
}